const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

// 页面文件
const pages = {};
// 配置 popup.html 页面
const chromeName = ["popup", "newTab"];

chromeName.forEach((name) => {
  pages[name] = {
    entry: `src/${name}/main.js`,
    template: `src/${name}/index.html`,
    filename: `${name}.html`,
  };
});

module.exports = {
  pages,
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.plugin("copy").use(CopyWebpackPlugin, [
      {
        patterns: [
          {
            from: path.resolve(`src/plugins/manifest.json`),
            to: `${path.resolve("dist")}/manifest.json`,
          },
          {
            from: path.resolve("src/assets"),
            to: path.resolve("dist/assets"),
          },
          {
            from: path.resolve("src/plugins/inject.js"),
            to: path.resolve("dist/js"),
          },
        ],
      },
    ]);

    // 启用热更新
    config.module
      .rule("vue")
      .use("vue-loader")
      .tap((options) => {
        options.hotReload = true;
        return options;
      });
  },
  // 配置 content.js background.js
  configureWebpack: {
    entry: {
      content: "./src/content/main.js",
      background: "./src/background/main.js",
    },
    output: {
      filename: "js/[name].js",
      chunkFilename: `[name].js`,
    },
    devtool: "inline-source-map",
  },

  // 配置 content.css
  css: {
    extract: {
      filename: "css/[name].css",
    },
  },
};
